package com.basic.business.service.impl;

import com.basic.business.model.entity.SysAccountRoleRel;
import com.basic.business.model.dto.SysAccountRoleRelDto;
import com.basic.business.model.vo.SysAccountRoleRelVo;
import com.basic.business.mapper.SysAccountRoleRelMapper;
import com.basic.business.service.SysAccountRoleRelService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.*;

import java.util.*;

import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * 登录账号 和 角色关系表(SysAccountRoleRel)表服务实现类
 *
 * @author huage
 * @since 2021-07-21 14:21:28
 */
@Service("sysAccountRoleRelService")
public class SysAccountRoleRelServiceImpl implements SysAccountRoleRelService {

    @Resource
    private SysAccountRoleRelMapper sysAccountRoleRelMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysAccountRoleRelVo findById(Integer id) {
        SysAccountRoleRel sysAccountRoleRel = this.sysAccountRoleRelMapper.selectById(id);

        //将SysAccountRoleRel转成SysAccountRoleRelVo
        SysAccountRoleRelVo sysAccountRoleRelVo = null;
        if (sysAccountRoleRel != null) {
            sysAccountRoleRelVo = new SysAccountRoleRelVo();
            BeanUtils.copyProperties(sysAccountRoleRel, sysAccountRoleRelVo);
        }

        return sysAccountRoleRelVo;
    }

    /**
     * 查询多条数据
     *
     * @param sysAccountRoleRelDto 实例对象
     * @param pageNum              页数
     * @param pageSize             每页条数
     * @return 对象列表
     */
    @Override
    public PageInfo<SysAccountRoleRelVo> findPageList(SysAccountRoleRelDto sysAccountRoleRelDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        //将SysAccountRoleRel转成SysAccountRoleRelVo
        SysAccountRoleRel sysAccountRoleRel = new SysAccountRoleRel();
        BeanUtils.copyProperties(sysAccountRoleRelDto, sysAccountRoleRel);
        List<SysAccountRoleRel> sysAccountRoleRelList = this.sysAccountRoleRelMapper.selectList(sysAccountRoleRel);

        PageInfo<SysAccountRoleRelVo> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(new PageInfo<>(sysAccountRoleRelList), pageInfo);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param sysAccountRoleRelDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysAccountRoleRelVo add(SysAccountRoleRelDto sysAccountRoleRelDto) {
        //将SysAccountRoleRelDto转成SysAccountRoleRel
        SysAccountRoleRel sysAccountRoleRel = new SysAccountRoleRel();
        BeanUtils.copyProperties(sysAccountRoleRelDto, sysAccountRoleRel);
        this.sysAccountRoleRelMapper.insert(sysAccountRoleRel);

        return this.findById(sysAccountRoleRel.getId());
    }

    /**
     * 修改数据
     *
     * @param sysAccountRoleRelDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysAccountRoleRelVo modify(SysAccountRoleRelDto sysAccountRoleRelDto) {
        //将SysAccountRoleRelDto转成SysAccountRoleRel
        SysAccountRoleRel sysAccountRoleRel = new SysAccountRoleRel();
        BeanUtils.copyProperties(sysAccountRoleRelDto, sysAccountRoleRel);
        this.sysAccountRoleRelMapper.update(sysAccountRoleRel);

        return this.findById(sysAccountRoleRel.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean remove(Integer id) {
        return this.sysAccountRoleRelMapper.deleteById(id) > 0;
    }
}

